package com.unsiv.helicopter.entity;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.Callback;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.widget.Toast;

import com.unsiv.helicopter.Command;
import com.unsiv.helicopter.MyAudioHelper;

/**
 * <p>
 * Button
 * </p>
 * 
 * @author fanzhen
 * @time 10:07:03 PM Oct 31, 2011
 * @email fanzhen.meng@gmail.com
 * @qq 474146502
 */
public class Button extends AnimatedSprite {
	public static final int BACK = 1;
	public static final int SENSOR = 2;
	public static final int RETURN = 3;
	public static final int LIGHT = 4;
	public static final int SWITCHER = 5;
	public static final int LOGO = 6;
	private int flag;// 按钮类型
	private Callback<Integer> callback;// 回调方法
	public static boolean isButtonSensorOn = false; // 重力感应状态
	public static boolean isAllowSensor = false;
	private Context context;
	long touchDownTime;

	public Button(float pX, float pY, float pTileWidth, float pTileHeight,
			TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
	}

	public Button(Context context, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion, int flag) {
		super(pX, pY, pTiledTextureRegion);
		this.flag = flag;
		this.context = context;
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
				pTouchAreaLocalY);
		if(!Command.needDoCommand){
			Toast.makeText(context, "Sorry please insert a handset.",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		touchDownTime = System.currentTimeMillis();
		AudioManager manager = (AudioManager) context
				.getSystemService(Activity.AUDIO_SERVICE);
		int maxv1 = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int volume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
		if (maxv1 != volume && this.flag != Button.BACK && this.flag != Button.LOGO) {
			Toast.makeText(context, "Sorry your device is not in max volume.",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			if (Button.BACK == this.flag) {
				this.setCurrentTileIndex(1);
				if (null != callback)
					callback.onCallback(2);
				break;
			}
			if (Button.SWITCHER == this.flag) {
				if (Command.needDoCommand) {
					if (0 == this.getCurrentTileIndex()) {
						this.setCurrentTileIndex(1);
						Command.allowCommand = true;
						MyAudioHelper.playCommand();
					} else {
						this.setCurrentTileIndex(0);
						Command.allowCommand = false;
					}
				} else {
					Command.allowCommand = false;
					Toast.makeText(
							context,
							"Sorry please insert a handset.",
							Toast.LENGTH_SHORT).show();
				}
				break;
			}
			if (!Command.allowCommand && Button.LOGO != this.flag) {
				Toast.makeText(
						context,
						"Sorry please turn on the swither.",
						Toast.LENGTH_SHORT).show();
				return false;
			}
			if (Button.SENSOR == this.flag) {
				if (0 == this.getCurrentTileIndex()) {
					this.setCurrentTileIndex(1);
					Button.isButtonSensorOn = true;
					if (null != callback)
						callback.onCallback(1);// 打开重力感应
				} else {
					this.setCurrentTileIndex(0);
					Button.isButtonSensorOn = false;
					if (null != callback)
						callback.onCallback(0);// 关闭重力感应
				}
			} else if (Button.RETURN == this.flag) {
				if (!Button.isButtonSensorOn) {
					if (0 == this.getCurrentTileIndex()) {
						this.setCurrentTileIndex(1);
						Button.isAllowSensor = true;
					} else {
						Button.isAllowSensor = false;
						this.setCurrentTileIndex(0);
					}
				}
			} else if (Button.LIGHT == this.flag) {
				if (0 == this.getCurrentTileIndex()) {
					this.setCurrentTileIndex(1);
					Command.mLampOnOff = 1;
				} else {
					this.setCurrentTileIndex(0);
					Command.mLampOnOff = 0;
				}
			}
		case TouchEvent.ACTION_MOVE:
			if (Button.LOGO == this.flag) {
				boolean run = true;
				while (run) {
					if((System.currentTimeMillis() - touchDownTime) > 2000L){
						callback.onCallback(6);
						run = false;
					}
				}
				break;
			}
		}
		return true;
	}

	public void setCallback(Callback<Integer> callback) {
		this.callback = callback;
	}
}